<?php
include_once 'curl.php';

class User {

	public $id;
	public $username;
	public $password;
	public $ime;
	public $ime_roditelja;
	public $jmbg;
	public $email;
	public $smer;
	public $pol;
	public $godina_studija;
	public $trenutna_godina_studija;
	public $admin;


	private $curl;
	private $user_agent;
	public $registrujKorisnika = false; # Ako je true, preusmeravamo na registraciju

	private $db;

	public function __construct($db){

		$curl = new \Zelenin\Curl;
		$this->curl = $curl;
		$this->user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0';

		$this->db = $db;

	}


	// Funkcija za login glavna
	public function login($username, $password){

		// proveri dal' korisnik postoji u bazi
		$proveraBaze = $this->db->select(	"korisnik",
											"username=:username AND password=:password",
											array(
												":username"	=>	$username,
												":password"	=>	$password
												));

		if(empty($proveraBaze)){
			$remoteLogin = $this->remoteLogin($username, $password, false, true);

			$_SESSION['ime_i_prezime'] = NULL;

			// Ako nije prosao login na servisu, vrati false
			if($remoteLogin == false)
				return false;
			// Ako je prosao login na remote, prikazujemo registraciju
			else {
				$this->username = $username;
				$this->postaviSesiju($username);
				$this->registrujKorisnika = true;
				$_SESSION['ime_i_prezime'] = $this->preuzmiIme($remoteLogin);
				return true;
			}


			return false;

		} else {
			$this->username = $username;
			$this->postaviSesiju($username);

			// Ako korisnik postoji, napravi sesiju
			return true;

		}


	}

	public function ulogovan(){
		if(isset($_SESSION['user']))
			return $_SESSION['user'];
	}

	// Funkcija za registraciju

	public function registracija($nizPodataka = array()){
		return $this->db->insert("korisnik", $nizPodataka);
	}

	// Funkcija za preuzimanje korisnikovog ID-a iz username-a

	public function preuzmiId($username) {
		$proveraBaze = $this->db->select(	"korisnik",
											"username=:username",
											array(
												":username"	=>	$username
												));
		// Proveri dal' ima rezultata
		if(isset($proveraBaze[0]['id']))
			return $proveraBaze[0]['id'];
		else
			return false;
	}

	// Preuzmi korisnika iz sesije
	public function preuzmiKorisnikaIzSesije(){
		if(!isset($_SESSION['user']))
			return false;

		return $this->preuzmiKorisnika($this->preuzmiId($_SESSION['user']));

	}

	// Funkcija za preuzimanje korisnikovog ID-a iz username-a

	public function preuzmiKorisnika($id) {
		$proveraBaze = $this->db->select(	"korisnik",
											"id=:id",
											array(
												":id"	=>	$id
												));
		// Proveri dal' ima rezultata
		if(isset($proveraBaze[0]['id']))
			return $proveraBaze[0];
		else
			return false;
	}

	public function remoteLogin($username, $password, $postaviSesiju = true, $responseReturn = false){

			// Formatiraj podatke
			$data = array(
			        'j_username' => $username,
			        'j_password' => $password
			);

			// Napravi zahtev
			$response = $this->curl
					       	 ->setUserAgent( $this->user_agent )
					        	//->setTimeout( 10 )
					         ->setCookieFile( dirname( __FILE__ ) . '/cookie.txt' )
					         ->post( 'https://student.fon.bg.ac.rs/j_spring_security_check', $data );

			if(!strpos($response['body'], "j_username")){

				// Autentifikacija uspela
				$this->username = $username;

				if($postaviSesiju)
					$this->postaviSesiju($username);

				if($responseReturn == true){
					return $response;
				} else {
					return true;
				}
			} else {
				// Neuspela
				return false;
			}
	}

	private function postaviSesiju($username){
		$_SESSION['user'] = $username;
	}

	public function preuzmiIme($response){
		$body = $response['body'];
		$matches = array();
		preg_match('/<strong class="m">(?s).*<\/strong>/i', $body, $matches);
		$ime_i_index = trim(Opste::cirilica_u_latinicu(Opste::html2txt($matches[0])));
		$ime = explode("(", $ime_i_index);
		return trim($ime[0]);
	}

}